﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using YiLe.BLL.Service;
using Spring.Context;
using YiLe.Properties;

namespace YiLe.View.Users
{
    public partial class UserListForm : Form
    {
        private IUserService userService;

        public UserListForm(IApplicationContext ctx)
        {
            InitializeComponent();
            userService = ctx["UserService"] as IUserService;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                string username = tbUserName.Text.Trim();
                var user = new Model.Domain.User { UserName = username };
                IList<Model.Domain.User> userList = userService.Search(user);
                bsUserList.DataSource = userList;
            }
        }

        private bool ValidateForm()
        {
            return true;
        }

        /// <summary>
        /// 用户列表读取事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void UserListForm_Load(object sender, EventArgs e)
        {
            IList<Model.Domain.User> userList = userService.GetAll();
            bsUserList.DataSource = userList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvUserList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex != -1)
            {

                //获取控件的值
                if (Boolean.Parse(dgvUserList.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()))
                {
                    dgvUserList.Rows[e.RowIndex].Cells[0].Value = 0;
                    dgvUserList.Rows[e.RowIndex].Selected = false;
                }
                else
                {
                    dgvUserList.Rows[e.RowIndex].Cells[0].Value = 1;
                    dgvUserList.Rows[e.RowIndex].Selected = true;
                }

                //MessageBox.Show(this.dgvUserList.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString()); //或者可以做其他事件处理程序
            }
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void add_Click(object sender, EventArgs e)
        {
            MainForm mainForm = MdiParent as MainForm;
            if (mainForm != null)
            {
                IApplicationContext ctx = mainForm.ApplicationContext;
                UserSaveForm saveForm = new UserSaveForm(ctx, this);
                saveForm.WindowState = FormWindowState.Normal;
                saveForm.StartPosition = FormStartPosition.CenterScreen;
                saveForm.TopMost = true;
                TopLevel = false;
                /*
             * saveForm.TopLevel = true;
            this.Controls.Add(saveForm);
            saveForm.Visible = true;
            saveForm.BringToFront();
            //SetParent((int)saveForm.Handle, (int)this.Handle);
             * 
            */
                saveForm.ShowDialog(MdiParent);
            }
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void modify_Click(object sender, EventArgs e)
        {
            if (bsUserList.Current == null)
            {
                MessageBox.Show(Resources.UserListForm_modify_Click_MODIFY);
            }
            else
            {
                Model.Domain.User user = bsUserList.Current as Model.Domain.User;
                MainForm mainForm = MdiParent as MainForm;
                if (mainForm != null)
                {
                    IApplicationContext ctx = mainForm.ApplicationContext;

                    UserSaveForm saveForm = new UserSaveForm(ctx, this, user);
                    //saveForm.MdiParent = this.ParentForm;
                    saveForm.ShowDialog(Parent);
                }
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            if (dgvUserList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请先选择要删除的记录");
            }//加入一个confirm弹出框
            else
            {
                //DialogResult dr = MessageBox.Show("确定要删除该记录", "提示", MessageBoxButtons.YesNo);
                //if (DialogResult.Yes == dr)
                if (MessageBox.Show("是否要删除该记录", "确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                {
                    foreach (DataGridViewRow row in this.dgvUserList.SelectedRows)
                    {
                        //TODO:判断该用户是否已经有订单等关联数据，有点话则不能删除用户，否则可以删除该用户
                        Model.Domain.User user = row.DataBoundItem as Model.Domain.User;
                        if (false) { }
                        else
                        {
                            userService.Delete(user);
                        }
                    }
                    UserListForm_Load(sender, e);//刷新list
                }
            }
        }

        private void close_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
